package com.qf.dao.impl;

import com.qf.dao.Card2Dao;
import com.qf.entity.Card2;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class Card2DaoImpl implements Card2Dao {
    QueryRunner queryRunner = new QueryRunner(JdbcUtils.ds);
    @Override
    public int add(Card2 card2) {
        String sql = "insert into t_card2 values (?,?,?,?,?,null,?,?)";
        try {
            return queryRunner.update(sql,card2.getId(),card2.getName(),card2.getId_num(),card2.getPhone(),card2.getPassword(),card2.getUid(),0.0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Card2 card2) {
        try {
            return queryRunner.update("update t_card2 set name = ?,id_num=?,phone = ?,password=?",card2.getName(),card2.getId_num(),card2.getPhone(),card2.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Card2 selectOne(String id) {
        try {
            return queryRunner.query("select * from t_card2 where id = ?",new BeanHandler<>(Card2.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int bingding(String id1,String id2) {
        String sql = "UPDATE t_card2 SET id_C1 = ? WHERE id = ?";
        try {
            return queryRunner.update(sql,id1,id2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Card2> selectByUid(String uid) {
        String sql = "select * from t_card2 where uid = ?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Card2.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateBalanceByID(double num, String id) {
        String sql = "UPDATE t_card2 SET balance = (balance + ?) where id = ?";
        try {
            return queryRunner.update(sql,num,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Card2 yanzheng(String id2, String name, String id1, String phone, String password) {
        String sql = "select * from t_card2 where id=? and name=? and id_C1=? and phone = ? and password = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Card2.class),id2,name,id1,phone,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Card2 yanzheng(String id2, String name) {
        String sql = "select * from t_card2 where id=? and name=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Card2.class),id2,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int zhifu(String id2, double money) {
        String sql = "update t_card2 set balance = balance - ? where id = ?";
        try {
            return queryRunner.update(sql,money,id2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Card2 findById(String id1) {
        String sql = "select * from t_card2 where id = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Card2.class),id1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int delete(String id) {
        String sql = "delete from t_card2 where id = ?";
        try {
            return queryRunner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Card2> findAllCard2(Integer uid) {
//        c2 join t_user u on c2.uid = u.id
        String sql = "SELECT * FROM t_card2 WHERE uid = ?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Card2.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int shoukuan(String id2, double money) {
        String sql = "update t_card2 set balance = balance + ? where id = ?";
        try {
            return queryRunner.update(sql,money,id2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
